<?php


namespace App\Http\Controllers\User;


use App\Http\Controllers\Controller;
use App\Models\User\Address;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;
/**
 * @group 地址信息
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class AddressController extends Controller
{
    /**
     * 地址查看
     *
     * @queryParam  address strings 地址信息
     * @queryParam  name string姓名
     * @queryParam  phone string 电话
     *
     *
     */
    public function index(Request $request){
        $type=$request->user_id;
        $query = Address::where('user_id',$type)->get()->toArray();
        return response()->json($query,200);
    }
    /**
     * 地址查看单条
     *
     * @@queryParam address string 地址信息
     * @queryParam  name string 姓名
     * @queryParam  phone string 电话
     *
     * @bodyParam  id int required 地址id
     */
    public function showone(Request $request){
        if ($id=$request->get('id')){
            $query = Address::find($id);
            return response()->json($query,200);
        }else{
            return response()->json(["message"=>"没有传id"],500);
        }

    }
    /**
     * 默认地址查看
     *
     * @queryParam address string 地址信息
     * @queryParam  name string 姓名
     * @queryParam  phone string 电话
     *
     * @bodyParam  id int required 地址id
     */
    public function showstatus(Request $request){
        $type=$request->user_id;
        $query = Address::where('user_id',$type)->where('status',1)->first()->toArray();
        if($query!=null){
            return response()->json($query,200);
        }
        return response()->json(["message"=>"没有默认地址"],500);

    }
    /**
     * 地址设置为默认
     * 504 修改失败
     */
    public function show(Request $request,$pk){
        $data = Address::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],406);
        }else{
            try{
                DB::beginTransaction();
                $row=Address::where(function ($q) use($data){
                    $q->where('user_id',$data['user_id']);
                    $q->where('status',1);
                })->first();
                if ($row!=null){
                    Address::where('id',$row['id'])->update(['status'=>0]);
                }
               Address::where('id',$pk)->update(['status'=>1]);
                DB::commit();
                return response()->json(["message"=>"修改成功"],200);
            }catch (QueryException $e){
                DB::rollBack();
                return response()->json(["message"=>"修改失败"],504);
            }
        }
    }
    /**
     * 地址添加
     *
     * @bodyParam  address string required 地址信息
     * @bodyParam  name string required 姓名
     * @bodyParam  phone string required 电话
     *
     * 504 添加失败
     */
    public function store(Request $request)
    {

        try{
            $e = genValidate();
            $validator = Validator::make($request->all(),$e);
            if($validator->fails()){
                return response()->json($validator->errors()->messages(),400);
            }
            $type=$request->user_id;
            $data=$request->all();
            $data['user_id'] =$type;
            Address::create($data);
            return response()->json(["message"=>"添加成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"添加失败"],504);
        }
    }
    /**
     * 地址修改
     * @bodyParam  address string required 地址信息
     * @bodyParam  name string required   姓名
     * @bodyParam  phone string required   电话
     *
     * 503 修改失败
     */
    public function update(Request $request, $pk){
        $e = genValidate();
        $data=$request->all();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json($validator->errors()->messages(),400);
        }
        try {
            $dd=Address::find($pk);
            if ($request->has('address')){
                $dd->address =$request->get('address');
            }
            if ($request->has('name')) {
                $dd->name = $request->get('name');
            }
            if ($request->has('phone')) {
                $dd->phone = $request->get('phone');
            }
            $dd->save();
            return response()->json(["message"=>"修改成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"修改失败"],503);
        }
    }
    /**
     * 地址删除
     *
     */
    public function destroy(Request $request,$pk){
        $data = Address::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        $data->delete();
        return response()->json(["message"=>"删除成功"],200);
    }
}